﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Delivery_IncasementList : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);

        if (!IsPostBack)
        {
            // 页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(),
                "IncasementList" }, Grv, new string[] { }, new int[] { Grv.Columns.Count - 1, Grv.Columns.Count - 1 }, CommonModel.loginOut1);

            QueryBind(0);

            DdlBind();
        }
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    
    public void QueryBind(int n)
    {
        string sql = @"select distinct top 500 CoCode, I.IncasementNo, I.UserCode, I.ContractNoMain, 
		I.SONo, I.ContractNoCust, I.AttachedFoldersName,
       I.AttachedFolderCreateTime,IFS.PlantSoNo,
		case I.IncasementType when 0 then '装箱单' else '装箱单汇总' end IncasementType 
        from	M_Incasement I
		left join M_IncasementFiles IFS on I.IncasementNo=IFS.IncasementNo 
		and I.ContractNoMain=IFS.ContractNoMain
        where I.DeleteFlag<>1 ";
        if (n == 1)
        {
            //if (!string.IsNullOrEmpty(txtDateFrom.Value) && !string.IsNullOrEmpty(txtDateTo.Value))
            //{
            //    sql += string.Format(" and convert(datetime,AttachedFolderCreateTime) between convert(datetime,'{0}')  and  convert(datetime,'{1}')",
            //        txtDateFrom.Value, txtDateTo.Value);
            //}
            //else if (!string.IsNullOrEmpty(txtDateFrom.Value))
            //{
            //    sql += string.Format(" and convert(datetime,AttachedFolderCreateTime)>convert(datetime,'{0}') ", txtDateFrom.Value);
            //}
            //else if (!string.IsNullOrEmpty(txtDateTo.Value))
            //{
            //    sql += string.Format(" and convert(datetime,AttachedFolderCreateTime)<convert(datetime,'{0}') ", txtDateTo.Value);
            //}

            if (!string.IsNullOrEmpty(txtIncasementNo.Text))
            {
                sql += string.Format(" and I.IncasementNo like '%{0}%' ", txtIncasementNo.Text);
            }
            if (!string.IsNullOrEmpty(txtSoNo.Text))
            {
                sql += string.Format(" and I.SONo like '%{0}%' ", txtSoNo.Text);
            }
            if (!string.IsNullOrEmpty(txtContractNo.Text))
            {
                sql += string.Format(" and I.ContractNoMain like '%{0}%' ", txtContractNo.Text);
            }
            if (!string.IsNullOrEmpty(txtContractNoCustomer.Text))
            {
                sql += string.Format(" and I.ContractNoMain like '%{0}%' ", txtContractNoCustomer.Text);
            }
            if (!ddlIncasementType.SelectedValue.Equals("-1"))
            {
                sql += string.Format(" and I.IncasementType='{0}'", ddlIncasementType.SelectedValue);
            }
            if (!string.IsNullOrEmpty(txtCustomer.Text))
            {
                sql += string.Format(" and exists (select distinct ContractNoMain from M_Customer Cust where I.ContractNoMain=Cust.ContractNoMain and Cust.CustomerName like '%{0}%')", txtCustomer.Text);
            }
            if (!string.IsNullOrEmpty(txtSaleName.Text))
            {
                sql += string.Format(" and exists (select distinct ContractNoMain from M_SalesCatalog SC where I.ContractNoMain=SC.ContractNoMain and SC.SalesName like '%{0}%')", txtSaleName.Text);
            }
            if (!string.IsNullOrEmpty(txtPlantSoNo.Text))
            {
                sql += string.Format(" and IFS.PlantSoNo like '%{0}%' ", txtPlantSoNo.Text);
            }
        }
        sql += " order	by I.IncasementNo  DESC, I.ContractNoMain  DESC,I.AttachedFolderCreateTime DESC";

        DataTable Dt = help.GetDataTable(sql);
        Grv.DataSource = Dt;
        Grv.DataBind();
        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            Grv.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = Grv.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        txtDateFrom.Value = "";
        txtDateTo.Value = "";
        txtContractNo.Text = "";
        txtIncasementNo.Text = "";
        txtSoNo.Text = "";
        txtCustomer.Text = "";
        txtSaleName.Text = "";
        ddlIncasementType.SelectedIndex = 0;
        HidQueryFlag.Value = "0";
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }

    protected void Grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grv.PageIndex = e.NewPageIndex;
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        catch { }
    }
    protected void Grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = Grv.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                Grv.PageIndex = pageindex - 1;
                //数据绑定
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }
    protected void Grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                ((LinkButton)e.Row.Cells[Grv.Columns.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除？');");
            }
        }
    }
    protected void Grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);

        string incasementNo = Grv.DataKeys[e.RowIndex]["IncasementNo"].ToString();

        string StrDelete = string.Format("Delete from M_Incasement where IncasementNo='{0}'", incasementNo);
        if (help.RunSqlInt(StrDelete) > 0)
        {
            CommonMethod.regAlertMessage("success！", this);

            IncasementModel model = new IncasementModel();
            IncasementDAO comDAO = new IncasementDAO();

            model.UserCode = Session["UserCode"].ToString();
            model.IncasementNo = incasementNo;
            model.SoNo = Grv.Rows[e.RowIndex].Cells[3].Text;
            model.date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            model.PlantSoNo = Grv.Rows[e.RowIndex].Cells[4].Text;
            model.ContractNoMain = Grv.Rows[e.RowIndex].Cells[2].Text;
            model.ContractNoCust = Grv.DataKeys[e.RowIndex]["ContractNoCust"].ToString();
            model.page = this;

            string plantSoNo = string.Empty;
            if (model.PlantSoNo != null && !model.PlantSoNo.Equals("-1") && model.PlantSoNo.Trim().Length > 0)
            { plantSoNo = " Plant SoNo: " + model.PlantSoNo; }

            model.EmailTittl = string.Format("装箱单汇总(ContractNo: {0}, Trading SoNo: {1} ,{2}, 装箱号：{3}, 删除时间：{4})",
                model.ContractNoMain, model.SoNo, plantSoNo, model.IncasementNo, model.date);
            model.EmailContent0 = string.Format("About Contract {0} have deleted", model.EmailTittl);

            comDAO.DeleteSentEmail(model);

            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }

        else
        {
            CommonMethod.regAlertMessage(" failed！", this);
        }
    }
       
    public void DdlBind()
    {
        ListItem item = new ListItem("请选择(Select Please)", "-1");
        DataTable[] dt = help.RunProcGetTables("GetddlForIncasementListQuery", null);
        ddlContractNo.DataSource = dt[0];
        ddlContractNo.DataValueField = "ContractNoMain";
        ddlContractNo.DataTextField = "ContractNoMain";
        ddlContractNo.DataBind();
        ddlContractNo.Items.Insert(0, item);

        ddlContractNoCustomer.DataSource = dt[0];
        ddlContractNoCustomer.DataValueField = "ContractNoMain";
        ddlContractNoCustomer.DataTextField = "ContractNoCust";
        ddlContractNoCustomer.DataBind();
        ddlContractNoCustomer.Items.Insert(0, item);

        ddlSoNo.DataSource = dt[1];
        ddlSoNo.DataValueField = "ContractNoMain";
        ddlSoNo.DataTextField = "SONo";
        ddlSoNo.DataBind();
        ddlSoNo.Items.Insert(0, item);

        ddlSaleName.DataSource = dt[2];
        ddlSaleName.DataValueField = "SalesName";
        ddlSaleName.DataTextField = "SalesName";
        ddlSaleName.DataBind();
        ddlSaleName.Items.Insert(0, item);

        ddlCustomer.DataSource = dt[3];
        ddlCustomer.DataValueField = "CustomerName";
        ddlCustomer.DataTextField = "CustomerName";
        ddlCustomer.DataBind();
        ddlCustomer.Items.Insert(0, item);

        ddlPlantSoNo.DataSource = dt[4];
        ddlPlantSoNo.DataValueField = "PlantSoNo";
        ddlPlantSoNo.DataTextField = "PlantSoNo";
        ddlPlantSoNo.DataBind();
        ddlPlantSoNo.Items.Insert(0, item);
    }
    protected void ddlContractNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string contractNo = ddlContractNo.SelectedValue.ToString();
        if (!contractNo.Equals("-1"))
        { txtContractNo.Text = contractNo; }
        else
        { txtContractNo.Text = ""; }
    }
    protected void ddlSoNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSoNo.SelectedValue.Equals("-1"))
        { txtSoNo.Text = ddlSoNo.SelectedItem.Text; }
        else
        { txtSoNo.Text = ""; }
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlCustomer.SelectedValue.Equals("-1"))
        { txtCustomer.Text = ddlCustomer.SelectedItem.Text; }
        else
        { txtCustomer.Text = ""; }
    }
    protected void ddlSaleName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSaleName.SelectedValue.Equals("-1"))
        { txtSaleName.Text = ddlSaleName.SelectedItem.Text; }
        else
        { txtSaleName.Text = ""; }
    }

    protected void ddlContractNoCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlContractNoCustomer.SelectedValue.Equals("-1"))
        { txtContractNoCustomer.Text = ddlContractNoCustomer.SelectedItem.Text; }
        else { txtContractNoCustomer.Text = ""; }
    }
    protected void ddlPlantSoNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlPlantSoNo.SelectedValue.Equals("-1"))
        { txtPlantSoNo.Text = ddlPlantSoNo.SelectedItem.Text; }
        else { txtPlantSoNo.Text = ""; }
    }
}